import React from 'react';
import { observable, action, toJS } from 'mobx';
import { Browser,Upload  } from 'weaver-mobile-page';
import { Tools, Modal, Toast, Bubble  } from 'weaver-mobile';
import * as adapter from '../../constants/adapter';
const alert = Modal.alert;
const getLabel = Tools.getLabel;
export default class BusinessCardStore {
    @observable buttonProps = { loading: false, disabled: false };
    @observable formParams = { fieldname_fullname: "", fieldname_jobtitle: "", fieldname_customerid: "", fieldname_customername: "", fieldname_phoneoffice: "", fieldname_email: "" };
    @observable browser  = new Browser.Store();
    @observable props;
    @observable showImage = false;
    @observable imageIndex;
    @observable complete = false;

    @observable datasImg = [];
    @observable uploadStore = new Upload.Store();

    @action
    initForm = (props) => {
        this.complete = true;
        this.props = props;
        const currentPath = props.location.pathname;
        this.browser = new Browser.Store({ route: currentPath });
        this.clearForm();
    }

    @action
    onChangeImg = (ids, datasImg) => {
        this.datasImg = datasImg;
        this.recognize();
    }

    @action
    recognize = () => {
        const file = toJS(this.datasImg)[0];
        if (file) {
            this.formParams = { fieldname_fullname: "", fieldname_jobtitle: "", fieldname_customerid: "", fieldname_customername: "", fieldname_phoneoffice: "", fieldname_email: "" };
            Bubble.warning(getLabel('388648','正在识别')+'...', 60, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2);
            let finish = false;
            Tools.callApi({
                url: '/api/crm/mobile/doCardRecognize',
                params: { imagefileid : file.fileid},
                method: 'POST'
            }).then((res) => {
                finish = true;
                Bubble.hide();
                if (res.status === "0") {
                    alert('',res.errMsg, [
                        {
                            text: 'ok',
                            onPress: () => { }
                        },
                    ])
                } else {
                    if (res.customerId) {
                        this.formParams.fieldname_customerid = res.customerId;
                        Bubble.success(getLabel('388615','已匹配客户')+":"+ res.customerName, 4, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2);
                    }
                    const cardInfo = JSON.parse(res.cardInfo);
                    //解析接口返回数据
                    const formatted_name = cardInfo.formatted_name;//姓名
                    if (formatted_name && formatted_name.length > 0) {
                        this.formParams.fieldname_fullname = formatted_name[0].item;
                    }
                    const title = cardInfo.title;//职称
                    if (title && title.length > 0) {
                        this.formParams.fieldname_jobtitle = title[0].item;
                    }
                    const organization = cardInfo.organization;//组织
                    if (organization && organization.length > 0) {
                        this.formParams.fieldname_customername = organization[1].item.name;
                    }
                    const telephone = cardInfo.telephone;//电话
                    if (telephone && telephone.length > 0) {
                        this.formParams.fieldname_phoneoffice = telephone[0].item.number;
                    }
                    const email = cardInfo.email;//邮箱
                    if (email && email.length > 0) {
                        this.formParams.fieldname_email = email[0].item;
                    }
                    // Toast.success('识别成功', 1);
                }
            });
            
            setTimeout(() => {
                if(!finish){
                    Bubble.warning(getLabel('387922','已超时'), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2);
                }
            }, 60000);
        }
    }




    @action
    buttonClick() {
        if (!this.formParams.fieldname_fullname) {
            Bubble.error("\""+getLabel('413','姓名')+"\""+getLabel('21423','未填写')+".",1, {top:'50%'} );
            return;
        }
        if (this.formParams.fieldname_customername) {
            this.buttonProps.loading = true;
            this.buttonProps.disabled = true;
            if (this.formParams.fieldname_customerid) {
                this.saveNewCustomer();
            } else {
                alert('',getLabel('22161','确定要提交吗?'), [
                    { text: getLabel('201','取消'), onPress: () => this.doCancel() },
                    { text: getLabel('83446','确定'), onPress: () => this.saveNewCustomer() },
                ])
            }
        } else {
            Bubble.error("\""+getLabel('1976','公司名称')+"\""+getLabel('21423','未填写')+".",1, {top:'50%'});
            return;
        }
    }

    doCancel = () => {
        this.buttonProps.loading = false;
        this.buttonProps.disabled = false;
    }

    saveNewCustomer = () => {
        Tools.callApi({
            url: '/api/crm/mobile/doCardSave',
            params: this.formParams,
            method: 'POST'
        }).then((res) => {
            if(res.status==="0"){
                Bubble.error(res.errMsg,2, {top:'50%'});
                this.buttonProps.loading = false;
                this.buttonProps.disabled = false;
            }else{
                let label = "";
                if(res.contacterId){
                    label = 572;
                }
                if(res.customerId){
                    label = 136;
                }
                window.crmGlobal && window.crmGlobal.doSearch();
                Bubble.success(getLabel(label,"数据")+getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2 , ()=>this.props.history.go(-1));
            }
        })
    }

    @action
    setFormParams(param) {
        this.formParams = { ...this.formParams, ...param };
    }

    clearForm() {
        this.datasImg = [];
        this.uploadStore = new Upload.Store();
        this.buttonProps = { loading: false, disabled: false };
        this.formParams = { fieldname_fullname: "", fieldname_jobtitle: "", fieldname_customerid: "", fieldname_customername: "", fieldname_phoneoffice: "", fieldname_email: "" };
    }

}